#!/bin/bash
# converting eps file to png

# anything @ $3 will flatten the image!
# $4 background color used in flattening

echo $@ >&2

FLATTEN=
BGC=

[[ -z $3 ]] || FLATTEN="-layers flatten"
[[ -z $4 ]] || BGC="-background $4"

get_image_size() {
	# prints width height of image
	identify $1 | awk '{s=$3;sub(/x/," ",s);print s}'
}

image_info() {
	sizeinfo=$(get_image_size $1)
	w=$(echo $sizeinfo|awk '{print $1}')
	h=$(echo $sizeinfo|awk '{print $2}')
	echo -e "<info file=\"$1\">\n<size width=\"$w\" height=\"$h\"/>\n</info>"
}

[[ -z $FLATTEN ]] || (
	echo "flattening image $2... $BGC"
) >&2
	
convert $FLATTEN $BGC $2 $1
image_info $1 > $1.info
